package com.yijiaganxi.services.web.article;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yijiaganxi.common.entity.article.YjgxArticle;
import org.yijiaganxi.common.res.R;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.yijiaganxi.common.GlobalConstants;
import com.yijiaganxi.services.service.article.YjgxArticleService;
import java.util.Date;
import javax.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;

/**
 * <p>
 * 文章管理 前端控制器
 * </p>
 *
 * @author zhangguowei123
 * @since 2020-08-29
 */
@Validated
@RestController
@RequestMapping("/services/yjgxArticle/" +GlobalConstants.WEB_FLAG )
public class YjgxArticleController {

	@Autowired
	public YjgxArticleService articleService;

	/**
	 * 获取所有文章
	 * 
	 * @return
	 */
	@GetMapping("getAllArticleList")
	public R getAllArticleList() {
		return R.opertionListResponse(articleService.selectList(new EntityWrapper<YjgxArticle>()));
	}

	/**
	 * 根据条件查询文章
	 * 
	 * @return
	 */
	@GetMapping("getArticleByCondition")
	public Object getArticleByCondition(@NotBlank(message = "文章名必填") String name) {
		EntityWrapper<YjgxArticle> entityWrapper = new EntityWrapper<YjgxArticle>();
		entityWrapper.eq("name", name);
		return R.opertionListResponse(articleService.selectList(entityWrapper));
	}

	/**
	 * 添加文章
	 * 
	 * @param banner
	 * @return
	 */
	@PostMapping("addArticle")
	public R addArticle(@Validated YjgxArticle article) {
		return R.insertOrUpdateResponse(articleService.insert(article));
	}

	/**
	 * 更新文章
	 * 
	 * @param banner
	 * @return
	 */
	@PostMapping("updateArticle")
	public R updateArticle(YjgxArticle article) {
		if (article.getId() == null)
			return R.error("Id 更新操作必填");
		article.setModifyDate(new Date());
		return R.insertOrUpdateResponse(
				articleService.update(article, new EntityWrapper<YjgxArticle>().eq("id", article.getId())));
	}

	/**
	 * 删除文章
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("deleteArticle")
	public R deleteArticle(@NotBlank(message = "id不能为空") String id) {
		return R.insertOrUpdateResponse(articleService.deleteById(id));
	}
}
